<template>
    <el-dialog :title="isEdit?'编辑场区管理':'新增场区管理'" :visible.sync="dialogForm" width="1100px">
        <el-form :inline="true" ref="form" :model="form" :rules="rules" class="add-form" label-width="110px">
            <el-row>
                <el-col :span="8">
                    <el-form-item label="场区" prop="areaNo">
                        <el-input v-model="form.areaNo" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="虚拟场区" prop="virtual">
                        <el-select v-model="form.virtual" placeholder="请选择" @change="handleSelect">
                            <el-option v-for="[key, val] in _dictTrueOrFalse" :key="key" :label="val"
                                       :value="key"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="最大贝位号" :prop="form.virtual?'':'maxBay'">
                        <el-input v-model="form.maxBay" @input="handleComputed" :disabled="form.virtual"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="最大排位数" :prop="form.virtual?'':'maxRow'">
                        <el-input v-model="form.maxRow" @input="handleComputed" :disabled="form.virtual"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="最大层高数" :prop="form.virtual?'':'maxTier'">
                        <el-input v-model="form.maxTier" @input="handleComputed" :disabled="form.virtual"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="贝号方向" :prop="form.virtual?'':'bayDirection'">
                        <el-select v-model="form.bayDirection" placeholder="请选择" :disabled="form.virtual">
                            <el-option v-for="[key, val] in _dictSOrB" :key="key" :label="val" :value="key"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>

            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="堆放顺序" :prop="form.virtual?'':'stackOrder'">
                        <el-select v-model="form.stackOrder" placeholder="请选择" :disabled="form.virtual">
                            <el-option v-for="[key, val] in _dictStackOrder" :key="key" :label="val"
                                       :value="key"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="允许出场" prop="allowedOut">
                        <el-select v-model="form.allowedOut" placeholder="请选择">
                            <el-option v-for="[key, val] in _dictTrueOrFalse" :key="key" :label="val"
                                       :value="key"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="标箱容量" prop="totalTeu">
                        <el-input v-model="form.totalTeu" disabled></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="所属单位" prop="orgId">
                        <auto-search-org v-model="form.orgId"
                                         @select="v=>{form.orgId=v;form.deptId='';form.deptName='';}"
                                         @selectName="v=>form.orgName=v"
                                         :disabled="true"
                                         :name="form.orgName"></auto-search-org>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="所属部门" prop="deptId">
                        <search-dept v-model="form.deptId"
                                     @select="v=>{form.deptId=v;}"
                                     @selectName="v=>form.deptName=v"
                                     :orgId="form.orgId"
                                     :disabled="!form.orgId"
                                     :name="form.deptName"></search-dept>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="场区说明" prop="remark">
                        <el-input type="textarea" :rows="3" style="width: 188px" placeholder="请输入内容"
                                  :maxLength="$maxLength"
                                  v-model="form.remark"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogForm = false">取 消</el-button>
            <el-button type="primary" @click="handleSubmit" :loading="$store.getters.loadingBtn">保 存</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import { addYardMsg, updateYardMsg } from "@/api/yardPlan/yardPlan";
    import autoSearchOrg from '@/components/autocomplete/autoSearchOrg'
    import searchDept from '@/components/autocomplete/autoSearchDept'
    import {REG_NUMBER,PRIVATE_BAY} from "@/utils/Reg";
    import { maxLength, sortLength } from "@/utils/rules";
    import {PRIVATE_AREA} from '@/utils/Reg'

    export default {
        name: "roleForm",
        components: { autoSearchOrg, searchDept },
        data() {
            return {
                form: this.clearForm(),
                rules: {
                    areaNo: [{ required: true, validator: PRIVATE_AREA, trigger: 'blur' },
                            {max:2,message:'最多输入2个字符',trigger:'blur'}],
                    maxBay: [{ required: true, validator:PRIVATE_BAY, trigger: 'blur' },],
                    maxRow: [{ required: true, message: '请输入正确的数字', pattern: REG_NUMBER, trigger: 'blur' }],
                    maxTier: [{ required: true, message: '请输入正确的数字', pattern: REG_NUMBER, trigger: 'blur' }],
                    bayDirection: [{ required: true, message: '请选择贝号方向', trigger: 'change' }],
                    stackOrder: [{ required: true, message: '请选择堆放顺序', trigger: 'change' }],
                    allowedOut: [{ required: true, message: '请选择是否出场', trigger: 'change' }],
                    virtual: [{ required: true, message: '请选择是否虚拟场区', trigger: 'change' }],
                    // orgId: [{ required: true, message: '请选择单位', trigger: 'change' }],
                    // deptId: [{ required: true, message: '请选择部门', trigger: 'change' }],
                    remark: [maxLength]
                },
                dialogForm: false,
                isEdit: false,
            }
        },
        methods: {
            clearForm() {
                return {
                    areaNo: "",
                    maxBay: "",
                    maxRow: "",
                    maxTier: "",
                    bayDirection: "FSTB",
                    virtual: false,
                    allowedOut: true,
                    orgId: this.$store.state.user.currentUser.org && this.$store.state.user.currentUser.org.id,
                    orgName: this.$store.state.user.currentUser.org && this.$store.state.user.currentUser.org.name,
                    deptId: "",
                    deptName: "",
                    stackOrder: "FTBR",
                    totalTeu: "",
                    controlfieldTeu: "",
                    remark: "",
                    enabledTeu: "",
                }
            },
            // 保存
            handleSubmit() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        this.isEdit ? this.handleToEdit() : this.handleToAdd()
                    } else {
                        this.$message({
                            message: '请检查表单',
                            type: 'error'
                        })
                    }
                })
            },
            // 新增
            handleToAdd() {
                addYardMsg(this.form).then(res => {
                    this.$message({ message: '新增成功!', type: 'success' })
                    this.dialogForm = false
                    this.$emit('load')
                })
            },
            // 编辑
            handleToEdit() {
                updateYardMsg(this.form.id, this.form).then(res => {
                    this.$message({ message: '编辑成功!', type: 'success' })
                    this.dialogForm = false
                    this.$emit('load')
                })
            },
            // 计算标箱容量
            handleComputed(val){
                if(this.form.maxBay%2!==0&&REG_NUMBER.test(this.form.maxRow)&&REG_NUMBER.test(this.form.maxTier)){
                    // 结果需要向下取整
                    this.form.totalTeu=Math.floor((this.form.maxBay/2+1)*this.form.maxRow*this.form.maxTier)
                }
            },
            // 选择虚拟场区
            handleSelect(val){
                if(val){
                    this.form.maxBay=''
                    this.form.maxRow=''
                    this.form.maxTier=''
                    this.form.bayDirection='FSTB'
                    this.form.stackOrder='FTBR'
                    this.form.totalTeu=''
                }
            }
        },
        watch: {
            dialogForm(bool) {
                if (!bool) {
                    this.$refs.form.resetFields()
                    this.isEdit = false
                    this.form = this.clearForm()
                    this.$emit('clear')
                }
            },
        },
    }
</script>

<style scoped>

</style>
